'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
	resultsuccess(data, message) {
		return this.ctx.body = {
			success: true,
			code: 200,
			message: message || '操作成功',
			data: data
		};
	}
	resultfail(message, code) {
		return this.ctx.body = {
			success: false,
			code: code || 300,
			message: message || '操作失败',
		};
	}
	constructor(ctx, app) {
		super(ctx, app); // 如果需要在构造函数做一些处理，一定要有这句话，才能保证后面 `this.ctx`的使用。
		// 就可以直接通过 this.ctx 获取 ctx 了
		// 还可以直接通过 this.app 获取 app 了
	}
	async list() {
		try {
			var userid = this.ctx.app.jwt.decode(this.ctx.headers.token).id;
			console.log('1111111', userid)
			const userList = await this.ctx.service.user.searchAll();
			this.ctx.body = {
				success: true,
				data: userList
			};
		} catch (error) {
			this.ctx.body = {
				success: false,
				error
			};
		}
	}
	async userinfo() {
		try {

			var username = this.ctx.app.jwt.decode(this.ctx.headers['x-dts-admin-token']).username;
			const user = await this.ctx.service.user.find(username);
			
			var roles = {};
			if(user.role_ids){
				roles = await this.ctx.service.user.findrole(user.role_ids);
			}
			var data = {
				...roles,
				"name": username,
				"avatar": user.avatar
			}
			this.resultsuccess(data)
		} catch (error) {
			this.ctx.body = {
				success: false,
				error
			};
		}
	}
}

module.exports = UserController;